<template>
  <view>
    <base-container style="padding: 0!important;">
      <view style="padding: 20rpx 0">
        <base-card :title="form.mdcInstitutionShortName">
          <template #extra>
            <uni-tag :text="form.mdcInstitutionType + '类'"
                     custom-style="background-color: #e6eeff; border-color: #e6eeff; color: #2c70fc;"
                     type="primary"/>
          </template>
          <view>
            <view>
              <text class="h_title">机构名称</text>
              <text>{{ form.mdcInstitutionName }}</text>
            </view>
            <view>
              <text class="h_title">机构地址</text>
              <text>{{ form.mdcInstitutionAddress }}</text>
            </view>
            <view>
              <text class="h_title">当前定位</text>
              <text class="bd">{{ location.address }}</text>
            </view>
            <view>
              <text class="h_title">电子围栏</text>
              <text>{{ form.electronicFence }}米</text>
            </view>
          </view>
        </base-card>
        <uni-forms ref="form" :border="true" :model="form" label-width="150">
          <uni-card is-full style="margin:30rpx 0;" title="收运信息">
            <uni-forms-item :rules="[{ required: true, errorMessage: '不能为空' }]" label="车牌号" name="vehicleId"
                            required>
              <base-picker v-model="form.vehicleId" :localdata="vehicleList"/>
            </uni-forms-item>
            <uni-forms-item :rules="[{ required: true, errorMessage: '不能为空' }]" label="驾驶员" name="driverId"
                            required>
              <base-picker v-model="form.driverId" :localdata="driverList"/>
            </uni-forms-item>
            <uni-forms-item label="装卸员" name="loUnPersonId">
              <base-picker v-model="form.loUnPersonId" :localdata="loUnPersonnelList"/>
            </uni-forms-item>
            <uni-forms-item :rules="[{ required: true, errorMessage: '不能为空' }]" label="收运人签字"
                            name="collectionTransportPerson"
                            required>
              <uni-easyinput v-model="form.collectionTransportPerson" :input-border="false" placeholder="请输入">
              </uni-easyinput>
            </uni-forms-item>
          </uni-card>

          <uni-card is-full>
            <uni-forms-item :rules="[{ required: true, errorMessage: '不能为空'}]" label="异常时间"
                            name="abnormalTime"
                            required>
              <uni-datetime-picker v-model="form.abnormalTime" :border="false" :clear-icon="false"
                                   :input-border="false" return-type="String" type="datetime"/>
            </uni-forms-item>
            <uni-forms-item :rules="[{ required: true, errorMessage: '不能为空' }]" label="异常类型"
                            name="abnormalType"
                            required>
              <base-picker v-model="form.abnormalType"
                           :localdata="[{text: '未开门', value: '未开门'},{text: '无医废', value: '无医废'}]"/>
            </uni-forms-item>
            <uni-forms-item label="异常原因" name="abnormalReason">
              <uni-easyinput v-model="form.abnormalReason" :input-border="false" maxlength="400" placeholder="请输入"
                             type="textarea"/>
            </uni-forms-item>
            <uni-forms-item label-width="0" name="abnormalPic">
              <base-images v-model="form.abnormalPic" title="上传图片"/>
            </uni-forms-item>
          </uni-card>
        </uni-forms>
      </view>

      <view class="foot-Btn">
        <view class="btn" @click="submit">提交</view>
      </view>
    </base-container>
  </view>
</template>

<script>
import {TaskPlanApi as Api} from "@/comms/api/taskPlanApi";
import {DateUtil, getUserName, LocationUtils, Msg, toBack} from "@/utils/BaseUtils";

export default {
  data() {
    return {
      params: {},
      form: {},
      driverList: [],
      location: {},
      loUnPersonnelList: [],
      vehicleList: []
    };
  },
  onLoad(params) {
    this.params = params;
  },
  mounted() {
    this.loadDate(this.params)
  },
  methods: {
    async loadDate({taskPlanId, mdcInstitutionId}) {
      Msg.loading(async close => {
        await Promise.all([
          Api.taskPlanVehicleList({taskPlanId}).then(({data}) => {
            this.vehicleList = (data || []).map(val => ({value: val.vehicleId, text: val.licensePlate}))
          }),
          Api.taskPlanDriverList({taskPlanId}).then(({data}) => {
            this.driverList = (data || []).map(val => ({value: val.ctPersonId, text: val.personName}))
          }),
          Api.taskLoUnPersonnelList({taskPlanId}).then(({data}) => {
            this.loUnPersonnelList = (data || []).map(val => ({value: val.ctPersonId, text: val.personName}))
          }),
        ]);
        if (taskPlanId && mdcInstitutionId) {
          await Api.medicalWasteTransferDetail({taskPlanId, mdcInstitutionId}).then(({data}) => this.form = {...data})
        }
        this.form.collectionTransportPerson = getUserName();
        this.form.abnormalTime = this.form.abnormalTime || DateUtil.getCurrDateTime()

        try {
          const location = await LocationUtils.getLocation();
          console.log('当前位置信息：', location);
          this.location = location;
        } catch (error) {
          Msg.error("获取定位信息时出错")
          console.error('获取定位信息时出错：', error);
        }
        close?.();
      })
    },

    submit() {
      Msg.loading(close => {
        this.$refs.form.validate().then(async (valid) => {
          Api.abnormal(this.convert())
              .then(res => {
                Msg.success("保存成功")
                uni.$emit('plannedRouterReload')
                setTimeout(() => toBack?.(), 1000)
              }).finally(() => close?.())
        }).catch(() => close?.())
      })
    },
    convert() {
      const {form = {}} = this;
      return {
        "id": form.id,
        "taskPlanId": form.taskPlanId,
        "mdcInstitutionId": form.mdcInstitutionId,

        vehicleId: form.vehicleId,
        driverId: form.driverId,
        transferPersonMobile: form.transferPersonMobile,
        loUnPersonId: form.loUnPersonId,
        collectionTransportPerson: form.collectionTransportPerson,
        abnormalTime: form.abnormalTime,
        abnormalType: form.abnormalType,
        abnormalReason: form.abnormalReason,
        abnormalPic: form.abnormalPic
      }
    }
  }
}
</script>

<style lang="scss">
.uni-card {
  margin-bottom: 15rpx;
}

.h_title {
  margin-right: 36rpx;
}

</style>
